//	Copyright (C) 2007 Christian Martick, http://juhu-mac.blogspot.com
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.

#import "smcTableCell.h"

@implementation smcTableCell

#pragma mark Helper

- (CGRect) _makeRectWithRect: (CGRect) rect
{
	CGRect ret = rect;
	ret.origin.x += 40.0f + 25.0f;
	ret.origin.y += 10.0f;
	ret.size.width -= 40.0f + 25.0f + 5.0f;
	ret.size.height -= 20.0f;
	return ret;
}

#pragma mark Sublass

- (void) dealloc
{
	[_location release];
	[_name release];
	[_text release];
	[_imageView release];
	[_content release];
	[super dealloc];
}

- (id) initWithFrame: (CGRect) theRect
{
	self = [super initWithFrame: theRect];
	if (self != nil)
	{
		_content = nil;
		CGRect rect = [self _makeRectWithRect: theRect];
		
		// name and relative time
		_name = [[UITextLabel alloc] initWithFrame:
			CGRectMake(rect.origin.x + 10.0f, rect.origin.y, rect.size.width - 10.0f - 10.f, 22.0f)];
		[_name setWrapsText: NO];
		[_name setBackgroundColor: [UIView colorWithRed: 1.0f green: 0.0f blue: 0.0f alpha: 0.0]];
		[_name setFont: [NSClassFromString(@"WebFontCache") createFontWithFamily: @"Helvetica" traits: 0 size: 14.0f]];
		[_name setColor: [UIView colorWithRed: 0.0f green: 0.0f blue: 0.0f alpha: 1.0]];
		[self addSubview: _name];

		// text
		_text = [[UITextLabel alloc] initWithFrame:
			CGRectMake(rect.origin.x + 10.0f, rect.origin.y + 22.0f + 5.0f, rect.size.width - 10.0f - 10.0f, 78.0f)];
		[_text setWrapsText:YES];
		[_text setBackgroundColor:[UIView colorWithRed: 0.0f green: 1.0f blue: 0.0f alpha: 0.0]];
		[_text setFont:  [NSClassFromString(@"WebFontCache") createFontWithFamily: @"Helvetica" traits: 0 size: 16.0f]];
		[_text setColor: [UIView colorWithRed: 0.0f green: 0.0f blue: 0.0f alpha: 1.0]];
		[_text setEllipsisStyle: 2];
		[_text setCentersHorizontally: NO];		
		[self addSubview: _text];

		// location
		_location = [[UITextLabel alloc] initWithFrame:
			CGRectMake(rect.origin.x + 0.6*rect.size.width, rect.size.height - 7.0f, 0.4*rect.size.width -10.0f, 22.0f)];
		[_location setWrapsText: NO];
		[_location setBackgroundColor: [UIView colorWithRed: 1.0f green: 0.0f blue: 0.0f alpha: 0.0]];
		[_location setFont: [NSClassFromString(@"WebFontCache") createFontWithFamily: @"Helvetica" traits: 1 size: 12.0f]];
		[_location setColor: [UIView colorWithRed: 0.0f green: 0.0f blue: 0.0f alpha: 1.0]];
		[self addSubview: _location];
		
		// icon
		_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5.0f, rect.origin.y + 0.5f*rect.size.height, 40.0f, 40.0f)];
		[self addSubview: _imageView];
	}
	return self;
}

- (void)drawRect:(struct CGRect) theRect
{
	//[super drawRect: theRect];
	
	CGRect rect = [self _makeRectWithRect: theRect];
	float radius = 20;
	
	CGColorRef white = [UIView colorWithRed:1.0f green:1.0f blue:1.0f alpha: 1.0f];
	CGColorRef lightgrey = [UIView colorWithRed: 0.95f green: 0.95f blue: 0.95f alpha: 1.0f];
	CGColorRef blue = [UIView colorWithRed: 0.30f green: 0.72f blue: 1.0f alpha: 1.0f];

	// main rounded with pointer
	CGContextSetFillColorWithColor(UICurrentContext(), white);
	
	UIBezierPath *bgPath = [UIBezierPath roundedRectBezierPath: rect withRoundedCorners: 15 withCornerRadius: radius];
	[bgPath moveToPoint: CGPointMake(rect.origin.x, rect.origin.y + 0.33f * rect.size.height)];
	[bgPath lineToPoint: CGPointMake(rect.origin.x - 7.0f, rect.origin.y + 0.5f* rect.size.height)];
	[bgPath lineToPoint: CGPointMake(rect.origin.x, rect.origin.y + 0.66f* rect.size.height)];
	[bgPath lineToPoint: CGPointMake(rect.origin.x, rect.origin.y + 0.33f* rect.size.height)];
	[bgPath fill];
	
	// name label rounded
	CGContextSetFillColorWithColor(UICurrentContext(), lightgrey);
	if ([self isSelected]) // highlight!
		CGContextSetFillColorWithColor(UICurrentContext(), blue);
	
	bgPath = [UIBezierPath roundedRectBezierPath: CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, 22.0f)
		withRoundedCorners: 3 withCornerRadius: radius];
	[bgPath fill];
	
	         
}
#pragma mark set & get

- (void) setContent: (NSDictionary *) obj
{
	[_content release];
	_content = [obj retain];
		
	[_name setText: [_content objectForKey: @"name"]];
	[_text setText: [_content objectForKey: @"text"]];
	[_location setText: [_content objectForKey: @"location"]];
	[_imageView setImage: [_content objectForKey: @"image"]];
	
	// adjust size for text 
	[_text sizeToFit];
	struct CGRect frame = [_text frame];
	float numberOfLines = ceil(frame.size.width / ([self _makeRectWithRect: [self frame]].size.width - 20.0f));
	frame.size.width = [self _makeRectWithRect: [self frame]].size.width - 20.0f;
	frame.size.height = frame.size.height * numberOfLines;
	if (frame.size.height > [self _makeRectWithRect: [self frame]].size.height - 22.0f - 5.0f - 10.0f)
		frame.size.height = [self _makeRectWithRect: [self frame]].size.height - 22.0f - 5.0f - 10.0f;
	[_text setFrame: frame];
	
	// adjust size & origin for location
	[_location sizeToFit];
	frame = [_location frame];
	frame.origin.x = [self _makeRectWithRect: [self frame]].size.width - 10.0f - frame.size.width + [self _makeRectWithRect: [self frame]].origin.x;
	if (frame.origin.x < 15)
	{
		frame.origin.x = [self _makeRectWithRect: [self frame]].origin.x + 15.0f;
		frame.size.width =  [self _makeRectWithRect: [self frame]].size.width - 20.0f;
	}
	[_location setFrame: frame];
	
	// adjust image origin
	frame = [_imageView frame];
	frame.origin.y -= 0.5f*[[_imageView image] size].height;
	[_imageView setFrame: frame];
}

- (NSDictionary *) content
{
	return _content;
}

@end
